/**
 * Created by zhanglihe on 15/11/3.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, template, Model, dialog, datepicker) {
    var sz_yyzz = 0;
    var sz_zzjg = 0;
    var sz_swdjz = 0;
    var contract = 0;
    var index = function () {
        this.init();
    };

    index.prototype = {
        init: function () {
            var _this = this;

            this.fetch();
        },
        fetch: function () {
            var that = this;
            var id = getUrlPath(3);
            this.model = new Model({
                url: _global.url.api + 'agent/apply_upload/' + id,
                data: {},
                render: function () {
                    $('#main-view').html(template('edit-tpl', this.data));
                    that.event();
                }
            });
            this.model.fetch();
            contract = this.model.data.contract;
        },
        set: function ($k, $v) {
            this[$k] = $v;
        },
        event: function () {
            var _this = this;

            $('input,select,textarea').on('change', function (e) {
                _this.change(e);
            });

            //保存事件
            $('#btn_save').on('click', function (e) {
                e.preventDefault();
                _this.model.data.sz_yyzz = sz_yyzz;
                _this.model.data.sz_zzjg = sz_zzjg;
                _this.model.data.sz_swdjz = sz_swdjz;
                _this.model.data.contract = contract;
                _this.model.data.status = 0;
                _this.model.data.email= $('#email').val();
                _this.model.data.password= $('#password').val();

                _this.model.valid_config = {
                    'email': 'email',
                    'password': 'required'
                };

                console.log('data',_this.model.data);

                if (_this.model.put()) {
                    //_this.fetch();
                }
            });


            $('body').on('click', '#btn-upload1', function (e) {
                _this.upload_dialog1 = dialog({
                    content: template('upload-tpl1', {}),
                    width: 550,
                    ok: function () {
                        var affix = $("#upload-frame1")[0].contentWindow.affix.get();
                        _this.affixPreview1(affix);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog1.show();
            });
            $('body').on('click', '#btn-upload2', function (e) {
                _this.upload_dialog2 = dialog({
                    content: template('upload-tpl2', {}),
                    width: 550,
                    ok: function () {
                        var affix = $("#upload-frame2")[0].contentWindow.affix.get();
                        _this.affixPreview2(affix);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog2.show();
            });
            $('body').on('click', '#btn-upload3', function (e) {
                _this.upload_dialog3 = dialog({
                    content: template('upload-tpl3', {}),
                    width: 550,
                    ok: function () {
                        var affix = $("#upload-frame3")[0].contentWindow.affix.get();
                        _this.affixPreview3(affix);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog3.show();
            });
            $('body').on('click', '#btn-upload4', function (e) {
                _this.upload_dialog4 = dialog({
                    content: template('upload-tpl4', {}),
                    width: 550,
                    ok: function () {
                        var affix = $("#upload-frame4")[0].contentWindow.affix.get();
                        _this.affixPreview4(affix);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog4.show();
            });
        },
        change: function (e) {
            this.model.data[$(e.target).attr('name')] = $(e.target).val();
        },

        affixPreview1: function (affix) {
            if (typeof affix.id !== 'undefined') {
                sz_yyzz = affix.id;
                $('#img-view1').html(template('affix-tpl', affix));
            }
        },
        affixPreview2: function (affix) {
            if (typeof affix.id !== 'undefined') {
                sz_zzjg = affix.id;
                $('#img-view2').html(template('affix-tpl', affix));
            }
        },
        affixPreview3: function (affix) {
            if (typeof affix.id !== 'undefined') {
                sz_swdjz = affix.id;
                $('#img-view3').html(template('affix-tpl', affix));
            }
        },
        affixPreview4: function (affix) {
            if (typeof affix.id !== 'undefined') {
                contract = affix.id;
                $('#img-view4').html(template('affix-tpl', affix));
            }
        }
    };

    new index();

});